package reader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

/**
 * this class should contain the file search and renaming logic code
 * @author naveen
 *
 */
public class ReadingMachineOld {
	
	private static String regex = "- " ,regex2 = "\\\\" ;
	private static final int limit = 3;
	
	/**
	 * this method is for finding files matching the criteria
	 *   in the specifies folder
	 * @param folderName
	 * @return
	 */
	public boolean readFolder(String folderName){		
		boolean result = false;
		System.out.println("************WELCOME!!**************");
		
		//ArrayList<String> criteria = new ArrayList<String>();
		String criteria = new String();
		//do{
			//criteria.add(JOptionPane.showInputDialog("enter the criteria to select the files"));
			
		//}while(true);
 		criteria = JOptionPane.showInputDialog("enter the criteria to select the files");
		
		System.out.println("reading files now.....");		
		File f = new File(folderName);		
		File files[] = f.listFiles();		
		//String newLine = System.getProperty("line.seperator");		
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter("matching file.txt",true));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		for(File file : files){			
			if(file.isFile()){								
				String path[] = file.getAbsolutePath().split(regex2);				
				System.out.println("------------------------------------");
				System.out.println("found a file named: "+file.getName());								
				//getting the filename and its extension
				String[] filename = file.getName().split("\\.");
				if(filename[0].contains(criteria))
				{					
				  //write into a txt file
					try {
						writer.write(file.getName());					
						writer.newLine();
						result = true;
					} catch (IOException e) {
						System.out.println("IOEXception while writing to txt file");
						e.printStackTrace();
					}					
				}
			}
		}
		System.out.println("*************OPERATION COMPLETED!!!************");
		try {
			writer.close();
		} catch (IOException e) {
			System.out.println("IO Exception at closing the writer");
			e.printStackTrace();
		}		
		
		return result;
	}
	
	
	/*
	String[] extension = file.getName().split("\\.");
	System.out.println("extension :"+ extension[1]);
	
	if(extension[1].equalsIgnoreCase("mp3")){
		String splitNames[] = path[3].split(regex, limit);
		String newFileName = path[0];
		newFileName = newFileName.concat("\\").concat(path[1].concat("\\").
				concat(path[2]).concat("\\").concat(splitNames[2]));
		System.out.println("file being renamed");
		File renamedFile = new File(newFileName);
		file.renameTo(renamedFile);
		System.out.println("New File Name :" + renamedFile.getName());
		System.out.println("------------------------------------");	
	}else System.out.println("file:" +file.getName()+" skipped");*/			
	
	/**
	 * this method is for renaming the files
	 * @return
	 */
	public boolean rename(){
		
		return true;
	}
    
	/**
	 * this method will be for moving the files
	 * @return
	 */
	public boolean move(){
		return true;
	}
	
	/**
	 * this method will be for removing the files 
	 * @return
	 */
	public boolean remove(){
		return true;
	}
}
